/**
 * Created by xuweijie on 17-5-17.
 */
// $('input[name=sex]:checked')

$(function () {
    $('#registerbtn').click(function () {
        var username=$('#username').val();
        var sex;
        $('input[name=sex]:checked').each(function () {
            sex = $(this).val();
        });
        var email=$('#email').val();
        var phone=$('#phone').val();
        var password=$('#password').val();
        if(validregister(username,sex,email,phone,password)){
            var params = $.param({
                username: username,
                sex:sex,
                email:email,
                phone:phone,
                password: password
            }, true);
            $.ajax({
                url:'/registerdata.action',
                type:'post',
                data:params,
                dataType:'json',
                success:function (data) {
                    if(data=="0"){
                        alert("邮箱已存在，请重新输入");
                    }else if(data=="1"){
                        alert("手机号已存在，请重新输入");
                    }else if(data=="2"){
                        window.location.href = "/front/frontlogin";
                    }else {
                        alert("注册失败");
                    }
                }
            });
        }else {
            alert("注册信息有误，请再次输入")
        }
    })
});

$(function () {
    $('#registerreset').click(function () {
        $('#username').val("");
        $('#email').val("");
        $('#phone').val("");
        $('#password').val("");
    })
});

function validregister(username,sex,email,phone,password) {
    var flag=true;
    if(username==="" || sex==="" ||email==="" ||phone==="" || password===""){
        flag=false;
    }
    if(email.indexOf('@')===-1){
        flag=false;
    }
    if(!(/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(phone))){
        flag=false;
    }
    return flag;
}